package nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Author: Dgg
 * @Date: 2021/12/3 21:57
 */
public class NIOclient {
	public static void main(String[] args) throws Exception {
		SocketChannel socketChannel = SocketChannel.open();

		socketChannel.configureBlocking(false);

		//提供服务器端ip 端口
		InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
		//链接服务器
		if (!socketChannel.connect(inetSocketAddress)) {
			while (!socketChannel.finishConnect()) {
				System.out.println("因为链接需要时间 客户端不需要阻塞 可以做其他工作。。。。。。");
			}
		}
		//、、、如果链接成功 就发送数据
		String str = "Hello Riley James~";
		// 根据放入的字节数组大小 确认buffer 大小
		ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());
		//发送buffer 将buffer数据写入channel
		socketChannel.write(wrap);
		System.in.read();
	}
}
